 package 
{
	import Base.Loader.SWFLoader;
	import Base.Net.NetEvent;
	import Base.Net.NetSocket;
	import Base.Sound.SoundManager;
	
	import Common.*;
	
	import Events.*;
	
	import config.GameConfig;
	
	import flash.display.*;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	
	import logic.*;
	
	import map.MapGrid;
	import map.PathFinder;
	
	import net.NetHandler;
	
	import ui.*;
	
	
	public class MainLib extends Sprite implements GameModule
	{
		public var netSocket:NetSocket = null;
		public var loader:SWFLoader = null;
		public var isInited:Boolean = false;
		public var bLoaded:Boolean = false;
		
		public function MainLib()
		{

			
		}
		
		
		public function init():Boolean
		{
			var act:GameAction = new GameAction();
			act = null;
			
			GameManager.Instance().init();
			GameCreatureManager.Instance().init();
			
			PathFinder.Instance().init();
			MapManager.Instance().init();
			
			
			BattleSession.Instance();
			GameScene.Instance();
			SceneManager.Instance();
			
			
			ModuleEventManager.instance.addEventListener( ModuleEvent.MAINSTAGE , onInit );
			ModuleEventManager.instance.addEventListener( ModuleEvent.MAINRELEASE , onRelease );
			
			netSocket = GameNetManager.instance.MainNetSocket;
			
			loadRes();
			
			GameFilters.init();
			
			return true;
		}
		
		
		public function release():void
		{
			
		}
		
		
		public function loadRes():void
		{
			if ( !loader )
			{
				loader = new SWFLoader();
				loader.contentLoaderInfo.addEventListener( Event.INIT , onLoaderInit );
				loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS , onSWFProgress );
				loader.Load( GameDefine.RESMAIN_SWF );
			}
			
		}
		
		
		/////////////////////////////////////////////////////////////////
		
		private function onClose( evnet:NetEvent ):void
		{
			//@ net error or close
			
			var moduleEvent:ModuleEvent = new ModuleEvent( ModuleEvent.MAINRELEASE );
			ModuleEventManager.instance.dispatchEvent( moduleEvent );
		}
		
		
		private function onLoaderInit( event:Event ):void
		{
			GameResManager.instance.mainRes = Object( loader.content ).getRes();
			
			loader.isLoading = false;
			loader.contentLoaderInfo.removeEventListener( Event.INIT , onLoaderInit );
			loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onSWFProgress );
			loader.Release();
			
			bLoaded = true;
			
			start();
			
			GameConfig.Instance().init();
		}
		
		
		private function start():void
		{
			if ( !isInited || !bLoaded )
			{
				return;
			}
			
			GameLoading.instance.load();
			GameLoading.instance.loading( "" , 0 );
			GameManager.instance.Run();
			
			NetHandler.Instance().init();
			
			BattleSession.instance.init();
			SceneManager.instance.init();
			GameScene.instance.init();
			
			GameUIManager.Instance().init();
			
			netSocket.addEventListener( NetEvent.CLOSE , onClose );
			netSocket.addEventListener( NetEvent.ERROR , onClose );
			
			// bug ..why render faild???? i don't know...
			//RenderManager.instance.Root.addChild( this );
			
			ModuleEventManager.instance.addEventListener( ModuleEvent.RESIZE , resizeHandler );
			ModuleEventManager.instance.addEventListener( ModuleEvent.KEYCODE , GameScene.instance.onKeyUp );
			ModuleEventManager.instance.addEventListener( ModuleEvent.KEYCODE , ChatUIHandler.instance.onKeyDown );
			
			resizeHandler( null );
		}
		
		
		private function onSWFProgress( event:ProgressEvent ):void
		{
			GameLoading.instance.loading( null , 100 * ( event.bytesLoaded / event.bytesTotal )  );
		}
		
		
		private function onInit( event:ModuleEvent ):void
		{
			GameLoading.instance.load();
			GameLoading.instance.loading( "" , 0 );
			
			var mainPlayer:GamePlayer = new GamePlayer();
			mainPlayer.init();
			mainPlayer.mainPlayer = true;
			
			GameManager.instance.setMainPlayer( mainPlayer );
			
			isInited = true;
			
			start();
		}
		
		
		private function onSecurityError( event:NetEvent ):void
		{
			
		}
		
		
		private function onRelease( event:ModuleEvent ):void
		{
			GameUIManager.instance.release();
			SceneManager.instance.release();
			
			netSocket.removeEventListener( NetEvent.CLOSE , onClose );
			netSocket.removeEventListener( NetEvent.ERROR , onClose );
			
			ModuleEventManager.instance.removeEventListener( ModuleEvent.RESIZE , resizeHandler );
			ModuleEventManager.instance.removeEventListener( ModuleEvent.KEYCODE , GameScene.instance.onKeyUp );
		}
		
		
		private function resizeHandler( event:ModuleEvent ):void
		{
			SceneManager.instance.resize();
			GameUIManager.instance.resize();
		}
	}
}
